home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Tracker 4.0 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  2.1 KB  |  115 lines  |  [TEXT/KAHL]

  1. /* getopt.c */
  2.  
  3. /* $Id: getopt.c,v 4.0 1994/01/11 17:48:21 espie Exp espie $
  4.  * $Log: getopt.c,v $
  5.  * Revision 4.0  1994/01/11  17:48:21  espie
  6.  * Small changes.
  7.  *
  8.  * Revision 1.2  1994/01/09  17:36:22  Espie
  9.  * Generalized open.c.
  10.  *
  11.  * Revision 1.1  1993/12/26  00:55:53  Espie
  12.  * Initial revision
  13.  *
  14.  * Revision 1.5  1993/12/04  16:12:50  espie
  15.  * New getopt semantics.
  16.  *
  17.  * Revision 1.4  1993/11/17  15:31:16  espie
  18.  * *** empty log message ***
  19.  *
  20.  * Revision 1.3  1993/01/26  14:10:38  espie
  21.  * Fixed up stupdi end of file bug.
  22.  *
  23.  * Revision 1.2  1992/11/27  10:29:00  espie
  24.  * General cleanup
  25.  *
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31.  
  32. #include "defs.h"
  33. #include "getopt.h"
  34.  
  35. ID("$Id: getopt.c,v 4.0 1994/01/11 17:48:21 espie Exp espie $")
  36. int optind = 1;
  37. char *optarg = 0;
  38. LOCAL not_an_option = 0;
  39.  
  40. LOCAL int parse_option(argv, option)
  41. char *argv[];
  42. struct long_option *option;
  43.     {
  44.     optind++;
  45.     if (option->argn)
  46.         optarg = argv[optind++];
  47.     return option->code;
  48.     }
  49.  
  50. int getlongopt(argc, argv, options)
  51. int argc;
  52. char *argv[];
  53. struct long_option *options;
  54.     {
  55.     if (not_an_option == optind)
  56.         return -1;
  57.     if (optind >= argc)
  58.         return -1;
  59.     if (argv[optind][0] == '-')
  60.         {
  61.         char *match = argv[optind]+1;
  62.         if (strlen(match) == 1)
  63.             {
  64.             if (match[0] == '-')
  65.                 {
  66.                 not_an_option = ++optind;
  67.                 return -1;
  68.                 }
  69.             while(options->fulltext)
  70.                 {
  71.                 if (options->abbrev == match[0])
  72.                     return parse_option(argv, options);
  73.                 else
  74.                     options++;
  75.                 }
  76.             return -1;
  77.             }
  78.         else
  79.             {
  80.             int max_match = 0;
  81.             struct long_option *best = 0;
  82.  
  83.             while (options->fulltext)
  84.                 {
  85.                 int i;
  86.                 for (i = 0; ; i++)
  87.                     {
  88.                     if (options->fulltext[i] == 0 && match[i] == 0)
  89.                         return parse_option(argv, options);
  90.                     if (match[i] == 0)
  91.                         {
  92.                         if (i > max_match)
  93.                             {
  94.                             max_match = i;
  95.                             best = options;
  96.                             }
  97.                         break;
  98.                         }
  99.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  100.                         break;
  101.                     }
  102.                 options++;
  103.                 }
  104.             if (max_match < 3)
  105.                 {
  106.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  107.                 return -1;
  108.                 }
  109.             return parse_option(argv, best);
  110.             }
  111.         }
  112.     else
  113.         return -1;
  114.     }
  115.